// import useMsg from '@/hooks/useMsg'
import { requireImg } from '@/utils/common'
import { debounce } from 'lodash'

/**
 * @param binding.value 激活拖拽功能的元素className
 */
export const VCopy = {
	mounted(el: HTMLElement, binding: any) {
		el.style.position = 'relative'
		const img = document.createElement('img')
		img.src = requireImg('module/tool/copy.png')
		img.title = '复制'
		img.style.cssText = `position:absolute;
                            top:.3906rem;
                            right:.3906rem;
                            cursor:pointer;
                            width:${binding.value ? binding.value : '.651rem'};
                            height:${binding.value ? binding.value : '.651rem'}`
		el.appendChild(img)
		img.addEventListener(
			'click',
			debounce(() => {
				const text = el.textContent!
				navigator.clipboard
					.writeText(text)
					.then(() => {
						// useMsg('复制成功！')
					})
					.catch((err) => {
						// useMsg('复制失败！')
						console.log('[ err ] >', err)
					})
			}, 200),
		)
	},
}
